using Microsoft.VisualStudio.GraphModel.CodeSchema;
using Microsoft.VisualStudio.Text;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using VSAdditionalCommands.Helpers;
using static System.Net.Mime.MediaTypeNames;

namespace VSAdditionalCommands.ToolWindows.ConstructorToolWindow
{
    public partial class ConstructorToolWindowControl : UserControl
    {
        public ConstructorToolWindowControl()
        {
            this.InitializeComponent();
            lstActions.Items.Add(new ListBoxItem() { Content="Вставить параметр контроллера и переменные",
                Name="InsertParam", ToolTip="Поместите курсор в конец параметров контроллера, введите имя параметра в поле toolbox."});
            lstActions.SelectedIndex = 0;
        }

        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            if (txtParameterName.Text.Length == 0)
                return;

            DocumentView docView = VS.Documents.GetActiveDocumentViewAsync().Result;
            if (docView?.TextView == null
                || docView.TextView.Selection.SelectedSpans.Count == 0
                || docView.TextBuffer == null) return;

            SnapshotPoint position = docView.TextView.Caret.Position.BufferPosition;

            string parameterNameFirstUpper = txtParameterName.Text.FirstCharToUpper();
            string parameterNameFirstLower = txtParameterName.Text.FirstCharToLower();

            docView.TextBuffer.Insert(position, $", I{parameterNameFirstUpper} {parameterNameFirstLower}");

            var snapshot = docView.TextBuffer.CurrentSnapshot;
            var text = snapshot.GetText(position, snapshot.Length- position);
            int foundPos = text.IndexOf('}');
            if (foundPos == -1)
                return;
            docView.TextBuffer.Insert(position + foundPos, $"\t_{parameterNameFirstLower} = {parameterNameFirstLower};\n\t\t");
            var searchText = "public";
            var prevTextReverse = new string(snapshot.GetText(0, position).ToCharArray().Reverse().ToArray());
            var searchTextReverse = new string(searchText.ToCharArray().Reverse().ToArray());
            foundPos = prevTextReverse.IndexOf(searchTextReverse);
            if (foundPos == -1) return;
            Match m = (new Regex(@";|{|}", RegexOptions.IgnoreCase)).Match(prevTextReverse, foundPos + searchTextReverse.Length);
            if (m.Success)
            {
                foundPos = m.Index;
            }
            docView.TextBuffer.Insert(position - foundPos, $"\n\t\tprivate readonly I{parameterNameFirstUpper} _{parameterNameFirstLower};");


        }

        private void txtParameterName_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if(e.Key == System.Windows.Input.Key.Enter)
            {
                btnGo_Click(null, null);
            }
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            txtParameterName.Focus();
        }
    }
}